import { put, delay, cancel, fork, take } from 'redux-saga/effects'
import { actionTypes, increase } from '../action/counter'

// 自动增加和停止的流程控制
function* autoFork() {
    while (true) {
        yield take(actionTypes.autoIncrease);
        const task = yield fork(function* () {
            while (true) {
                yield delay(2000)
                yield put(increase())
            }
        })
        yield take(actionTypes.stopAutoIncrease);
        yield cancel(task)
    }
}

export default function* () {
    yield fork(autoFork)
}